<?php
namespace org\kahikai\database\fs;

require_once dirname(__FILE__) . '/../PictureDao.php';
require_once dirname(__FILE__) . '/../../domain/Picture.php';

use org\kahikai\database\PictureDao;
use org\kahikai\domain\Picture;

class FsPictureDao implements PictureDao {
	private $picsDir;
	
	public function __construct() {
		$this->picsDir = dirname(__FILE__) . '/../../../../pics';
	}
	/**
	 *
	 * @param int $speciesId a species id
	 * @return an array of pictures by development step for specified species (never null even if species id is unknown)
	 */
	public function getBySpeciesId($speciesId) {
		$pictures = array();
			
		$speciesDir = $this->picsDir . '/' . $speciesId;
		if(is_dir($speciesDir)) {
			foreach (scandir($speciesDir) as $pic) {
				if(preg_match('/(\d+)\..+/', $pic, $matches) > 0) {
					$pictures[$matches[1]] = new Picture($speciesId . '_' . $matches[1], 'pics/' . $speciesId . '/' . $pic);
				}
			}
		}
		
		return $pictures;
	}
	
	/**
	 *
	 * @param int $developmentStepId a DevelopmentStep id
	 * @return the Picture of provided DevelopmentStep or null
	 */
	public function getByDevelopmentStepId($developmentStepId) {
		$pictures = array();
		
		foreach (scandir($this->picsDir) as $speciesId) {
			if($speciesId != '.' || $speciesId != '..') {
				$speciesDir = $this->picsDir . '/' . $speciesId;
				if(is_dir($speciesDir)) {
					foreach (scandir($speciesDir) as $pic) {
						if(preg_match("/$developmentStepId\..+/", $pic) > 0) {
							$pictures[$speciesId] = new Picture($speciesId . '_' . $developmentStepId, 'pics/' . $speciesId . '/' . $pic);
						}
					}
				}
			}
		}
		
		return $pictures;
	}
}
?>